<?php
namespace Library\Route;

class RouteChecker implements RouteCheckInterface
{

    /**
     * 检查是否符合表达示
     * @param $params
     * @param $patterns
     * @return bool
     */
    public static function check_pattern($params, $patterns)
    {
        foreach($params as $key => $val) {
            foreach($patterns as $kk => $vv) {
                if($kk == $key) {
                    $ok = preg_match($vv, $val);
                    if($ok == 0) {
                        // 未能匹配到
                        return false;
                    }
                }
            }
        }
        return true;
    }

    /**
     * 检查 callback 是否为一个 闭包方法
     * @param $callback
     * @return bool
     */
    public static function check_callback_closure($callback)
    {
        return ($callback instanceof \Closure) ? true : false;
    }

    /**
     * 检查请求方式是否正确
     * @param $http_method
     * @param null $need
     * @return bool
     */
    public static function check_http_method($http_method, $need = null)
    {
        return ((strtolower($http_method) === $need) or (null == $need)) ? true : false;
    }


    /**
     * 检查路由是否正确
     * @param $route
     * @return bool
     */

    public static function check_route($route)
    {
        $route = (string) $route;
        if($route == '/') {
            return true;
        } else if (strpos($route, '/') !== false){
            return true;
        }

        return false;
    }

}